/**
 * Copyright © 2020 易久批信息技术有限公司. All rights reserved.
 */
package com.dysm.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author xiay
 * @since 2020/12/9 9:55 下午
 */
public class DateUtil {

    private static final String DATETIME_FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";
    private static final String YMD_FORMAT_STRING = "yyyy-MM-dd";
    private static final String HMS_FORMAT_STRING = "HH:mm:ss";

    public DateUtil() {
    }

    public static Date getBeginTime(Date beginTime) {
        if (beginTime != null) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(beginTime);
            cal.set(11, 0);
            cal.set(12, 0);
            cal.set(13, 0);
            cal.set(14, 0);
            return cal.getTime();
        } else {
            return null;
        }
    }

    public static Date getWeeklyBeginTime(Date beginTime) {
        if (beginTime != null) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(beginTime);
            cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
            cal.set(7, 2);
            return cal.getTime();
        } else {
            return null;
        }
    }

    public static Date getMonthlyBeginTime(Date beginTime) {
        if (beginTime != null) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(beginTime);
            cal.set(cal.get(1), cal.get(2), cal.get(5), 0, 0, 0);
            cal.set(5, cal.getActualMinimum(5));
            return cal.getTime();
        } else {
            return null;
        }
    }

    public static Date getEndTime(Date endTime) {
        if (endTime != null) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(endTime);
            cal.set(11, 23);
            cal.set(12, 59);
            cal.set(13, 59);
            return cal.getTime();
        } else {
            return null;
        }
    }

    public static Date getWeeklyEndTime(Date endTime) {
        if (endTime != null) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(getWeeklyBeginTime(endTime));
            cal.add(7, 6);
            cal.set(11, 23);
            cal.set(12, 59);
            cal.set(13, 59);
            return cal.getTime();
        } else {
            return null;
        }
    }

    public static Date getMonthlyEndTime(Date endTime) {
        if (endTime != null) {
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(endTime);
            int MaxDay = calendar.getActualMaximum(5);
            calendar.set(calendar.get(1), calendar.get(2), MaxDay, 23, 59, 59);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date getMonthEndTime(Date endTime) {
        if (endTime != null) {
            Calendar calendar = Calendar.getInstance();
            int MaxDay = calendar.getActualMaximum(5);
            calendar.set(calendar.get(1), calendar.get(2), MaxDay, 23, 59, 59);
            return calendar.getTime();
        } else {
            return null;
        }
    }

    public static Date getFormatBeginTime(String source) {
        return getBeginTime(parseDate(source));
    }

    public static Date getFormatEndTime(String source) {
        return getEndTime(parseDate(source));
    }

    public static String formatDate(Date date) {
        String dateStr = null;
        if (date == null) {
            return null;
        } else {
            try {
                SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                dateStr = dateTimeFormat.format(date);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return dateStr;
        }
    }

    public static String formatHMS(Date date) {
        String dateStr = null;
        if (date == null) {
            return null;
        } else {
            try {
                SimpleDateFormat hmsFormat = new SimpleDateFormat("HH:mm:ss");
                dateStr = hmsFormat.format(date);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return dateStr;
        }
    }

    public static String formatYMD(Date date) {
        String dateStr = null;
        if (date == null) {
            return null;
        } else {
            try {
                SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
                dateStr = ymdFormat.format(date);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return dateStr;
        }
    }

    public static String formatByType(Date date, String type) {
        String dateStr = null;
        if (date == null) {
            return null;
        } else {
            try {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
                dateStr = simpleDateFormat.format(date);
            } catch (Exception var4) {
                var4.printStackTrace();
            }

            return dateStr;
        }
    }

    public static Date parseDate(String source) {
        Date date = null;
        if (source == null) {
            return null;
        } else {
            try {
                SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                date = dateTimeFormat.parse(source);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return date;
        }
    }

    public static Date converterToYMD(String source) {
        Date date = null;
        if (source == null) {
            return null;
        } else {
            try {
                SimpleDateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd");
                date = ymdFormat.parse(source);
            } catch (Exception var3) {
                var3.printStackTrace();
            }

            return date;
        }
    }

    public static Date addSecondNum(Date date, int secondNum) {
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(13, secondNum);
            return calendar.getTime();
        }
    }

    public static Date addMinuteNum(Date date, int minuteNum) {
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(12, minuteNum);
            return calendar.getTime();
        }
    }

    public static Date addDayNum(Date date, int dayNum) {
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(5, dayNum);
            return calendar.getTime();
        }
    }

    public static Date addMonthNum(Date date, int monthNum) {
        if (date == null) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(2, monthNum);
            return calendar.getTime();
        }
    }

    public static String dataToTime(Date date) {
        if (date == null) {
            return null;
        } else {
            long time = date.getTime();
            return String.valueOf(time);
        }
    }

    public static Date stringToDate(String date, String type) {
        if (date != null) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(type);
                return sdf.parse(date);
            } catch (Exception var3) {
                var3.printStackTrace();
            }
        }

        return null;
    }

    public static Long getDayNum(Date beginTime, Date endTime) {
        Long millisecond = endTime.getTime() - beginTime.getTime();
        Long dayNum = (millisecond - 1L) / 86400000L;
        return dayNum + 1L;
    }
}
